package com.easy.controller;

import com.easy.bean.Member;
import com.easy.bean.PrivateClass;
import com.easy.common.CommonResult;
import com.easy.service.IMemberService;
import com.easy.service.IPrivateService;
import com.easy.utils.Page;
import com.easy.utils.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.awt.color.CMMException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class MemberController {

    @Autowired
    IMemberService memberS;

    @Autowired
    IPrivateService privateS;

    @GetMapping("member")
    public CommonResult getMember(Member member, Page page){
        List<Member> list = memberS.getMember(member,page);
        int count = memberS.getMemberCount(member);
        HashMap map = new HashMap<>();
        map.put("list", list);
        map.put("count", count);
        return CommonResult.success(map);
    }

    @DeleteMapping("member/{id}")
    public CommonResult delMemberByID(@PathVariable int id){
        List<PrivateClass> list = privateS.getAllPrivate();
        for(PrivateClass p : list){
            if(p.getMemberID() == id){
                return CommonResult.fail(400,"会员有课不能删除");
            }
        }
        int res = memberS.delMemberByID(id);
        return CommonResult.success(res);
    }

    @PostMapping("member")
    public CommonResult addMember(@RequestBody Member member){
        List<Member> list = memberS.getAllMember();
        for(Member m : list){
            if(m.getPhoneNumber().equals(member.getPhoneNumber())){
                return CommonResult.fail(400,"该电话号码已存在");
            } else if (m.getEmail().equals(member.getEmail())) {
                return CommonResult.fail(400,"该电子邮箱已存在");
            }else if (m.getNationalIDNumber().equals(member.getNationalIDNumber())) {
                return CommonResult.fail(400,"该身份证号已存在");
            }
        }
        memberS.addMember(member);
        member = memberS.getMemberByID(member.getMemberID());
        return CommonResult.success(member);
    }

    @PutMapping("member")
    public CommonResult editMember(@RequestBody Member member){
        List<Member> list = memberS.getAllMember();
        for(Member m : list){
            if(m.getPhoneNumber().equals(member.getPhoneNumber())&&m.getMemberID() != member.getMemberID()){
                return CommonResult.fail(400,"该电话号码已存在");
            } else if (m.getEmail().equals(member.getEmail())&&m.getMemberID() != member.getMemberID()) {
                return CommonResult.fail(400,"该电子邮箱已存在");
            }else if (m.getNationalIDNumber().equals(member.getNationalIDNumber())&&m.getMemberID() != member.getMemberID()) {
                return CommonResult.fail(400,"该身份证号已存在");
            }
        }
        memberS.editMember(member);
        member = memberS.getMemberByID(member.getMemberID());
        return CommonResult.success(member);
    }

    @GetMapping("membertype")
    public CommonResult getMemberType(){
        List<Type> list = memberS.getMemberType();
        return CommonResult.success(list);
    }

    @GetMapping("memberstatustype")
    public CommonResult getMemberStatusType(){
        List<Type> list = memberS.getMemberStatusType();
        return CommonResult.success(list);
    }
}
